--
-- Author: yawei.zhu
-- Date: 2019-01-03 17:12:02
--
-- PvpChampionFormPagePanel
--
local PvpChampionFormRender = import(".renders.PvpChampionFormRender")

local ClassRef = app.mvc.createPanelClass(...)

local total_render_count = 15
local arrLeftIndex = {1, 9, 13, 15}

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.ChampionFormNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    
    local teamPanel = self._ui.Panel_team
    local arrRenders = {}
    for i = 1, total_render_count do
        local node = teamPanel:getChildByName("t_"..i)
        local render = PvpChampionFormRender.create(node)
        render:setNeedInfo(i, self)
        table.insert(arrRenders, render)

        -- 点击事件
        local panel = node:getChildByName("Panel")
        panel:addClickEventListener(function()
            local data = render:getData()
            if data then
                executeFunc(self._onOperate, "showTeamDetailInfo", data.teamId)
            end
        end)
    end
    self._teamRenders = arrRenders
end

function ClassRef:getBattleResult(teamInfo1, teamInfo2)
    if not self._battleResults then return end
    if not teamInfo1 or not teamInfo2 then return end
    local teamId1 = teamInfo1.teamId
    local teamId2 = teamInfo2.teamId
    if not teamId1 or not teamId2 then return end

    for _, data in ipairs(self._battleResults) do
        if (data.TeamA == teamId1 and data.TeamB == teamId2) or (data.TeamA == teamId2 and data.TeamB == teamId1) then
            return data
        end
    end
end

function ClassRef:getTeamInfo(idx)
    return self._teamInfos[idx]
end

function ClassRef:refresh()
    if not self._data then return end

    local teams = self._data[1]
    local myTeamId = self._data[2]
    local bStrongZone = self._data[4]
    self._battleResults = self._data[3]
    self._teamInfos = {}

    for i = 1, 8 do
        local teamInfo = teams[i]
        if teamInfo then 
            local idx = i
            local round = teamInfo.round
            if round > 3 then round = 3 end
    
            for j = 0, round do
                local startIndex = 0
                local nodeIndex = 0
                startIndex = arrLeftIndex[j + 1]
                nodeIndex = startIndex + idx - 1
                self._teamInfos[nodeIndex] = teamInfo
    
                idx = math.ceil(idx / 2)
            end
        end
    end

    for i = 1, total_render_count do
        local render = self._teamRenders[i]
        -- 设置需要的数据
        render:setNeedInfo(i, self, myTeamId)
        render:setData(self._teamInfos[i])
    end

    -- 显示比分
    for i = 9, 15 do
        local preIndex1 = (i - 8) * 2 - 1
        local preIndex2 = (i - 8) * 2

        local info1 = self._teamInfos[preIndex1]
        local info2 = self._teamInfos[preIndex2]

        local battleResult = self:getBattleResult(info1, info2)
        if battleResult then
            -- 赢的队伍比分放前面
            local scoreL = battleResult.ScoreA
            local scoreR = battleResult.ScoreB
            if battleResult.WonTeam == battleResult.TeamB then
                scoreL = battleResult.ScoreB
                scoreR = battleResult.ScoreA
            end
            self._teamRenders[i]:setScore(scoreL, scoreR)
        else
            -- donothing
        end
    end

    -- 判断是否为最强赛区
    if bStrongZone then
        self._ui.Panel_1:setVisible(true)
        -- 判断是否产生了冠军
        if self._teamInfos[15] then
            self._teamRenders[15]:setVisible(true)
            self._teamRenders[15]:setChampionFlag()
        else
            self._teamRenders[15]:setVisible(false)
        end
    else
        self._ui.Panel_1:setVisible(false)
        self._teamRenders[15]:setVisible(true)
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
